package com.easyadmin.pro.common.generator;

import com.easyadmin.pro.common.controller.BaseController;
import com.easyadmin.pro.common.entity.BaseEntity;
import com.mybatisflex.codegen.Generator;
import com.mybatisflex.codegen.config.GlobalConfig;
import com.zaxxer.hikari.HikariDataSource;

/**
 * packageName com.easyadmin.pro.common.generator
 *
 * @author 骑着蚂蚁去上天
 * @version JDK 17
 * @className MybatisFlexGenerator
 * @date 2024/4/1
 * @description mybatis-flex 代码生成器
 */
public class MybatisFlexGenerator {

    /**
     * 数据源
     */
    private final static String URL = "jdbc:mysql://127.0.0.1:3306/easy-admin-pro?useUnicode=true&characterEncoding=utf8&useSSL=true&serverTimezone=GMT";

    /**
     * 账号
     */
    private final static String USER_NAME = "root";

    /**
     * 密码
     */
    private final static String PASSWORD = "123456";

    /**
     * 模块名称
     */
    private static String MODULE = ".sys";

    /**
     * 根包
     */
    private static String BASE_PACKAGE_PATH = "com.easyadmin.pro.modules".concat(MODULE);

    /**
     * 设置xml生成路径
     */
    private static String MAPPER_XML_PATH = System.getProperty("user.dir")
            + "/src/main/java/".concat(BASE_PACKAGE_PATH).concat(".mapper.xml");


    public static void main(String[] args) {
        //配置数据源
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setJdbcUrl(URL);
        dataSource.setUsername(USER_NAME);
        dataSource.setPassword(PASSWORD);

        //创建配置内容
        GlobalConfig globalConfig = createGlobalConfigUseStyle();

        //通过 datasource 和 globalConfig 创建代码生成器
        Generator generator = new Generator(dataSource, globalConfig);

        //生成代码
        generator.generate();
    }

    /**
     * 生成器策略
     * @return
     */
    private static GlobalConfig createGlobalConfigUseStyle() {
        //创建配置内容
        GlobalConfig globalConfig = new GlobalConfig();

        //设置根包
        globalConfig.getPackageConfig()
                .setBasePackage(BASE_PACKAGE_PATH)
                .setMapperXmlPath(MAPPER_XML_PATH.replace(".", "/"));

        //设置表前缀和只生成哪些表，setGenerateTable 未配置时，生成所有表
        globalConfig.getStrategyConfig()
                .setTablePrefix("sys_")
                .setGenerateTable("sys_data_dictionary");

        //设置生成 entity 并启用 Lombok
        globalConfig.enableEntity()
                .setWithLombok(true)
                .setClassSuffix("Entity")
                .setSuperClass(BaseEntity.class)
                .setJdkVersion(17);

        // 设置生成controller
        globalConfig.enableController()
                .setSuperClass(BaseController.class);
        // 设置生成service
        globalConfig.enableService()
                .setClassPrefix("I");
        // 设置生成service实现
        globalConfig.enableServiceImpl();
        // 设置生成mapper接口
        globalConfig.enableMapper()
                .setMapperAnnotation(Boolean.TRUE);
        // 设置生成mapper.xml文件
        globalConfig.enableMapperXml();

        //设置生成 mapper
        globalConfig.enableMapper();

        return globalConfig;
    }
}